package com.example.dao;

import java.util.List;
import java.util.Map;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.example.domain.TransferClassesPrice;

@Repository
public interface TransferClassesPriceDao extends JpaRepository<TransferClassesPrice, Integer> {

    @Modifying
    @Transactional
    Integer deleteByTransferPriceId(Integer transferPriceId);

    List<TransferClassesPrice> findByTransferPriceId(Integer transferPriceId);

    @Modifying
    @Transactional
    Integer deleteByTransferPriceIdIn(List<Integer> listId);

    @Query(nativeQuery = true, value = "SELECT u.account as userName,u.nickName,tcp.*,tc.originalClass,tc.nowClass,ct.typeName \r\n" +
            "FROM ((transfer_classes_price tcp INNER JOIN transfer_class tc ON tcp.transfer_Id = tc.transfer_class_Id)\r\n" +
            "INNER JOIN users u ON u.user_Id = tc.user_Id)\r\n" +
            "INNER JOIN charge_type ct ON ct.charge_type_Id = tcp.charge_type_Id")
    List<Map<String, Object>> queryAllStudentAndTransferClassPriceInfo();

    @Query(nativeQuery = true, value = "SELECT u.account as userName,u.nickName,tc.originalClass,tc.nowClass,tc.transfer_class_Id as transferId \r\n" +
            "FROM transfer_class tc INNER JOIN users u ON tc.user_Id = u.user_Id \r\n" +
            "WHERE tc.transferStatus = '已批阅' \r\n" +
            "AND tc.transfer_class_Id not in (SELECT transfer_Id FROM transfer_classes_price)")
    List<Map<String, Object>> queryAllStudentAndTransferClassInfo();

    @Query(nativeQuery = true, value = "SELECT u.account as userName,u.nickName,tcp.*,tc.originalClass,tc.nowClass \r\n" +
            "FROM (transfer_classes_price tcp INNER JOIN transfer_class tc ON tcp.transfer_Id = tc.transfer_class_Id)\r\n" +
            "INNER JOIN users u ON u.user_Id = tc.user_Id \r\n" +
            "where tc.nowClass LIKE CONCAT('%',:nowClass,'%')")
    List<Map<String, Object>> findByNowClassLike(@Param("nowClass") String nowClass);

    @Query(nativeQuery = true, value = "SELECT u.account as userName,u.nickName,tcp.*,tc.originalClass,tc.nowClass \r\n" +
            "FROM (transfer_classes_price tcp INNER JOIN transfer_class tc ON tcp.transfer_Id = tc.transfer_class_Id)\r\n" +
            "INNER JOIN users u ON u.user_Id = tc.user_Id \r\n" +
            "where tc.originalClass LIKE CONCAT('%',:originalClass,'%')")
    List<Map<String, Object>> findByOriginalClassLike(@Param("originalClass") String originalClass);

    @Query(nativeQuery = true, value = "SELECT u.account as userName,u.nickName,tcp.*,tc.originalClass,tc.nowClass \r\n" +
            "FROM (transfer_classes_price tcp INNER JOIN transfer_class tc ON tcp.transfer_Id = tc.transfer_class_Id)\r\n" +
            "INNER JOIN users u ON u.user_Id = tc.user_Id \r\n" +
            "where tcp.originalClassMoney LIKE CONCAT('%',:originalClassMoney,'%')")
    List<Map<String, Object>> findByOriginalClassMoneyLike(@Param("originalClassMoney") String originalClassMoney);

    @Query(nativeQuery = true, value = "SELECT u.account as userName,u.nickName,tcp.*,tc.originalClass,tc.nowClass \r\n" +
            "FROM (transfer_classes_price tcp INNER JOIN transfer_class tc ON tcp.transfer_Id = tc.transfer_class_Id)\r\n" +
            "INNER JOIN users u ON u.user_Id = tc.user_Id \r\n" +
            "where tcp.nowClassMoney LIKE CONCAT('%',:nowClassMoney,'%')")
    List<Map<String, Object>> findByNowClassMoneyLike(@Param("nowClassMoney") String nowClassMoney);

    @Query(nativeQuery = true, value = "SELECT u.account as userName,u.nickName,tcp.*,tc.originalClass,tc.nowClass \r\n" +
            "FROM (transfer_classes_price tcp INNER JOIN transfer_class tc ON tcp.transfer_Id = tc.transfer_class_Id)\r\n" +
            "INNER JOIN users u ON u.user_Id = tc.user_Id \r\n" +
            "where tcp.priceSpread LIKE CONCAT('%',:priceSpread,'%')")
    List<Map<String, Object>> findByPriceSpreadLike(@Param("priceSpread") String priceSpread);

    @Query(nativeQuery = true, value = "SELECT u.account as userName,u.nickName,tcp.*,tc.originalClass,tc.nowClass,ct.typeName  \r\n" +
            "FROM ((transfer_classes_price tcp INNER JOIN transfer_class tc ON tcp.transfer_Id = tc.transfer_class_Id) \r\n" +
            "INNER JOIN users u ON u.user_Id = tc.user_Id) \r\n" +
            "INNER JOIN charge_type ct ON ct.charge_type_Id = tcp.charge_type_Id \r\n" +
            "WHERE u.user_Id = :userId")
    List<Map<String, Object>> selectTransferTuitionByUserId(@Param("userId") Integer userId);

}
